/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlaufgabe3;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JPanel;

/**
 * View nach MVC Pattern. Stellt ein Tortendiagramm dar.
 * 
 * @author Adrian Cieluch
 * @version 1.0, 11/3/11
 */
public class PieChartView extends JPanel implements Observer {
    /**
     * Speichert Model
     */
    private AccountModel model;
    
    /**
     * Speichert Tortendiagrammausschnitte.
     */
    private ArrayList<PieSlice> slices = new ArrayList<PieSlice>();
    
    /**
     * Macht das Model bekannt.
     * 
     * @param m AccountModel
     */
    public PieChartView(AccountModel m) {
        model = m;
        
        for(int i = 0; i < model.getAccountQuantity(); i++) {
            slices.add(new PieSlice(model.getAccountBalanceByIndex(i), model.getColorByIndex(i)));
        }
        
        this.setBounds(0, 0, this.getHeight(), this.getHeight());
    }
    
    /**
     * Zeichnet Hintergrund und Tortendiagramm.
     * 
     * @param g Grafikkontext
     */
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        
        Rectangle r = this.getBounds();
        r.x -= r.x;
        
        g2d.setColor(Color.lightGray);
        g2d.fillRect((int)r.x, (int)r.y, (int)r.width, (int)r.height);
        
        drawPie(g2d, r);
    }
    
    /**
     * Logik zum Zeichnen der Teile des Tortendiagramms.
     * 
     * @param g Grafikkontext
     * @param area Zeichenflaeche
     */
    public void drawPie(Graphics2D g, Rectangle area) {
        // Get total value of all slices
        area.x += area.width * 0.3;
        area.height -= 5;
        area.width = area.height;
        double total = 0.0D;
        for (int i=0; i < slices.size(); i++) {
            total += slices.get(i).getSize();
        }

        // Draw each pie slice
        double curValue = 0.0D;
        int startAngle = 0;
        for (int i=0; i < slices.size(); i++) {
            // Compute the start and stop angles
            startAngle = (int)(curValue * 360 / total);
            int arcAngle = (int)(slices.get(i).getSize() * 360 / total);

            // Ensure that rounding errors do not leave a gap between the first and last slice
            if (i == slices.size()-1) {
                arcAngle = 360 - startAngle;
            }

            // Set the color and draw a filled arc
            g.setColor(slices.get(i).getColor());
            if(slices.get(i).getSize() != 0)
                g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);

            curValue += slices.get(i).getSize();
        }
    }
    
    /**
     * Updatemethode des Observers. Aktualisiert Werte fuer das Tortendiagramm
     * und zeichnet sich neu.
     * 
     * @param obs Das zu beobachtende Model
     * @param obj Beliebiges Objekt
     */
    @Override
    public void update(Observable obs, Object obj) {
        AccountModel model = (AccountModel)obs;
        
        for(int i = 0; i < slices.size(); i++) {
            slices.set(i, new PieSlice(model.getAccountBalanceByIndex(i), model.getColorByIndex(i)));
        }
        
        repaint();
    }
    
}
